//global
var game = null;

/*function switchMenus(fromMenu, toMenu, callback) {
    $(fromMenu).animate({
        "margin-left": "-=1500px"
    }, 400, function () {
        //$(fromMenu).attr('visibility', 'hidden');
        //$(toMenu).show();
        $(toMenu).css("margin-left", "-1500px");
        $(toMenu).animate({
            "margin-left": "+=1500px"
        }, 400, callback);
    });
};*/

$(document).ready(function() {
	game = new gameEngine(gameMode.multiPlayer);
	/*$('#mainMenu').children().children().click(function(event) {
		switchMenus('#mainMenu', '#game', function() {
			game = new gameEngine(gameMode[this.id]);
		});
	});*/

	$('#main').mousedown(function(event) {
		var mouseX = event.pageX - this.offsetLeft;
		var mouseY = event.pageY - this.offsetTop;
		switch(event.which) {
			case 1:
				if(!game.graphicsEngine.structureToBuild) {
					this.mouseDown = true;
					this.selectRectX = mouseX;
					this.selectRectY = mouseY;
					this.selectRectEndX = this.selectRectX;
					this.selectRectEndY = this.selectRectY;
				}
				else {
					var buildTypeId = gameObjectTypes[game.graphicsEngine.structureToBuild.templateName];
					var tileX = Math.floor((game.graphicsEngine.cameraX + game.graphicsEngine.structureToBuild.x) / game.map.tileSize);
					var tileY = Math.floor((game.graphicsEngine.cameraY + game.graphicsEngine.structureToBuild.y) / game.map.tileSize);
					game.networkingEngine.queueBuildStructure(buildTypeId, tileX, tileY);
				}
				break;
			case 3:
				var tileX = (mouseX + game.graphicsEngine.cameraX) / game.map.tileSize;
				var tileY = (mouseY + game.graphicsEngine.cameraY) / game.map.tileSize;
				game.graphicsEngine.structureToBuild = null;
				if(game.selected.length > 0) {
					game.networkingEngine.queueMove(tileX, tileY, game.selected);
				}
				break;
		}
	});

	$('#main').mousemove(function(event) {
		var mouseX = event.pageX - this.offsetLeft;
		var mouseY = event.pageY - this.offsetTop;

		if(this.mouseDown) {
			this.selectRectEndX = mouseX;
			this.selectRectEndY = mouseY;
		}
		if(game.graphicsEngine.structureToBuild) {
			game.graphicsEngine.structureToBuild.x = mouseX;
			game.graphicsEngine.structureToBuild.y = mouseY;
		}
	});

	$('#main').mouseup(function(event) {
		switch(event.which) {
			case 1:
				this.mouseDown = false;

				//evil hack to get around scoping issues (JS is retarded)
				game.recalculateSelected(Math.min(this.selectRectX, this.selectRectEndX),
					Math.min(this.selectRectY, this.selectRectEndY),
					Math.max(this.selectRectEndX, this.selectRectX),
					Math.max(this.selectRectEndY, this.selectRectY));

				this.selectRectX = 0;
				this.selectRectY = 0;
				this.selectRectEndX = 0;
				this.selectRectEndY = 0;
		}
	});

	$('#miniMap').mousedown(function(event) {
		var mouseX = event.pageX - this.offsetLeft;
		var mouseY = event.pageY - this.offsetTop;
		switch(event.which) {
			case 1:
				this.mouseDown = true;
				game.graphicsEngine.putCamera(mouseX / this.width * game.map.pixelWidth - $('#main')[0].width/2,
					mouseY / this.height * game.map.pixelHeight - $('#main')[0].height/2);
				break;
			case 3:
				var tileX = mouseX / this.width * game.map.width;
				var tileY = mouseY / this.height * game.map.height;
				game.networkingEngine.queueMove(tileX, tileY, game.selected);
				break;
				break;
		}
	});

	$('#miniMap').mousemove(function(event) {
		var mouseX = event.pageX - this.offsetLeft;
		var mouseY = event.pageY - this.offsetTop;
		if(this.mouseDown) {
			game.graphicsEngine.putCamera(mouseX / this.width * game.map.pixelWidth - $('#main')[0].width/2,
				mouseY / this.height * game.map.pixelHeight - $('#main')[0].height/2);
		}
	});

	$('#miniMap').mouseup(function(event) {
		switch(event.which) {
			case 1:
				this.mouseDown = false;
				break;
		}
	});

    $('#chat_input').keypress(function(event) {
		switch(event.which) {
			case 13:
				game.networkingEngine.sendChatMessage();
				break;
		}
	});

	for(var i = 0; i < 5; i++) {
		$('#cancelButtons').html($('#cancelButtons').html() + '<button type="button" name="'+i+'" style="width: 32px; height: 20px;"></button>');
	}
	$('#cancelButtons').children().click(function(event) {
		if(game.selected.length > 0) {
			var selectedUnit = game.selected[0];
			var index = this.name * 1;
			game.networkingEngine.queueCancelUnit(selectedUnit.id, index);
		}

	});

	$(document).keydown(function(event) {
		var scrollSpeed = 40;
		switch(event.which) {
			case keys.cameraUp:
				game.graphicsEngine.moveCamera(0, -scrollSpeed);
				break;
			case keys.cameraDown:
				game.graphicsEngine.moveCamera(0, scrollSpeed);
				break;
			case keys.cameraLeft:
				game.graphicsEngine.moveCamera(-scrollSpeed, 0);
				break;
			case keys.cameraRight:
				game.graphicsEngine.moveCamera(scrollSpeed, 0);
				break;
		}
	});
});

var keys = {
	cameraUp: 87,
	cameraDown: 83,
	cameraLeft: 65,
	cameraRight: 68
};
